package com.ruoyi.project.business.batch.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.business.batch.domain.YlPickCodeBatch;
import com.ruoyi.project.business.batch.service.IYlPickCodeBatchService;
import com.ruoyi.project.business.machine.domain.YlMachine;
import com.ruoyi.project.business.machine.service.IYlMachineService;
import com.ruoyi.project.business.product.domain.YlProduct;
import com.ruoyi.project.business.product.service.IYlProductService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 取货码批次Controller
 * 
 * @author ruoyi
 * @date 2021-04-26
 */
@Controller
@RequestMapping("/business/batch")
public class YlPickCodeBatchController extends BaseController
{
    private String prefix = "business/batch";

    @Autowired
    private IYlPickCodeBatchService ylPickCodeBatchService;
    
    @Autowired 
    private IYlProductService ylProductService;
    
    @Autowired
    private IYlMachineService ylMachineService;

    @RequiresPermissions("business:batch:view")
    @GetMapping()
    public String batch()
    {
        return prefix + "/batch";
    }

    /**
     * 查询取货码批次列表
     */
    @RequiresPermissions("business:batch:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YlPickCodeBatch ylPickCodeBatch)
    {
        startPage();
        List<YlPickCodeBatch> list = ylPickCodeBatchService.selectYlPickCodeBatchList(ylPickCodeBatch);
        return getDataTable(list);
    }

    /**
     * 导出取货码批次列表
     */
    @RequiresPermissions("business:batch:export")
    @Log(title = "取货码批次", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YlPickCodeBatch ylPickCodeBatch)
    {
        List<YlPickCodeBatch> list = ylPickCodeBatchService.selectYlPickCodeBatchList(ylPickCodeBatch);
        ExcelUtil<YlPickCodeBatch> util = new ExcelUtil<YlPickCodeBatch>(YlPickCodeBatch.class);
        return util.exportExcel(list, "取货码批次数据");
    }

    /**
     * 新增取货码批次
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	//查出所有的商品和机器给前台
    	YlProduct ylProduct = new YlProduct();
    	List<YlProduct> products = ylProductService.selectYlProductList(ylProduct);
    	mmap.put("products", products);
    	
    	YlMachine ylMachine = new YlMachine();
    	List<YlMachine> machines = ylMachineService.selectYlMachineList(ylMachine);
    	mmap.put("machines", machines);
    	
        return prefix + "/add";
    }

    /**
     * 新增保存取货码批次
     */
    @RequiresPermissions("business:batch:add")
    @Log(title = "取货码批次", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YlPickCodeBatch ylPickCodeBatch)
    {
        return toAjax(ylPickCodeBatchService.insertYlPickCodeBatch(ylPickCodeBatch));
    }

    /**
     * 修改取货码批次
     */
    @GetMapping("/edit/{batchId}")
    public String edit(@PathVariable("batchId") Long batchId, ModelMap mmap)
    {
        YlPickCodeBatch ylPickCodeBatch = ylPickCodeBatchService.selectYlPickCodeBatchById(batchId);
        mmap.put("ylPickCodeBatch", ylPickCodeBatch);
        return prefix + "/edit";
    }

    /**
     * 修改保存取货码批次
     */
    @RequiresPermissions("business:batch:edit")
    @Log(title = "取货码批次", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YlPickCodeBatch ylPickCodeBatch)
    {
        return toAjax(ylPickCodeBatchService.updateYlPickCodeBatch(ylPickCodeBatch));
    }

    /**
     * 删除取货码批次
     */
    @RequiresPermissions("business:batch:remove")
    @Log(title = "取货码批次", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ylPickCodeBatchService.deleteYlPickCodeBatchByIds(ids));
    }
    
    /**
     * 停用取货码批次
     */
    @RequiresPermissions("business:batch:block")
    @Log(title = "取货码批次", businessType = BusinessType.DELETE)
    @PostMapping( "/block")
    @ResponseBody
    public AjaxResult block(String ids)
    {
        return toAjax(ylPickCodeBatchService.blockYlPickCodeBatchByIds(ids));
    }
    
    /**
     * 启用取货码批次
     */
    @RequiresPermissions("business:batch:block")
    @Log(title = "取货码批次", businessType = BusinessType.DELETE)
    @PostMapping( "/active")
    @ResponseBody
    public AjaxResult active(String ids)
    {
        return toAjax(ylPickCodeBatchService.activeYlPickCodeBatchByIds(ids));
    }
}
